//
//  MyInformationTextViewSheet.m
//  Yanwei
//
//  Created by Seven-Augus on 2017/5/22.
//  Copyright © 2017年 DCloud. All rights reserved.
//

#import "MyInformationTextViewSheet.h"
#import "NSString+Util.h"

@interface MyInformationTextViewSheet () <UITextViewDelegate> {
    BOOL _firstAppear;
}

@property (nonatomic, strong) UITextView *personalSignatureTV;
@property (nonatomic, strong) UIButton *surePersonalSignatureButton;

@end

@implementation MyInformationTextViewSheet

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 0, ScreenWidth, 145);
        self.backgroundColor = Color244;
        _firstAppear = YES;
        [self initView];
    }
    return self;
}

/** 初始化数据 */
- (void)initView {
    // 计算键盘高度
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppears:) name:UIKeyboardWillShowNotification object:nil];
    
    self.personalSignatureTV.backgroundColor = [UIColor whiteColor];
    self.surePersonalSignatureButton.backgroundColor = Color217;
}

- (void)setPlaceholder:(NSString *)placeholder {
    [self.personalSignatureTV setValue:placeholder forKeyPath:@"placeholder"];
}


#pragma mark - setting
- (void)setTextViewAlreadyString:(NSString *)textViewAlreadyString {
    _textViewAlreadyString = textViewAlreadyString;
    if (_textViewAlreadyString.length > 0) {
        self.personalSignatureTV.text = _textViewAlreadyString;
    }
}


#pragma mark - progress
- (void)progressSurePersonalSignatureButton {
    if (self.delegate && [self.delegate respondsToSelector:@selector(myInforMationTextViewSheetString:)]) {
        [self.delegate myInforMationTextViewSheetString:self.personalSignatureTV.text];
    }
    [self hideView];
}


#pragma mark - <UITextViewDelegate>
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (range.location >= 60) {
        [SVProgressHUD showErrorWithStatus:@"最大字数，60字！"];
        return NO;
    }
    return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView {

}
- (void)textViewDidChange:(UITextView *)textView {
    [self addObserverValueDidChanged];
}
- (void)addObserverValueDidChanged {
    if ([NSString isBlankString:self.personalSignatureTV.text]) {
        self.surePersonalSignatureButton.backgroundColor = Color217;
        _surePersonalSignatureButton.userInteractionEnabled = NO;
    } else {
        self.surePersonalSignatureButton.backgroundColor = Color175030;
        _surePersonalSignatureButton.userInteractionEnabled = YES;
    }
}


#pragma mark - 监听键盘高度
/** 键盘出现 */
- (void)keyboardWillAppears:(NSNotification *)notification {
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGFloat keyBoardEndY = value.CGRectValue.origin.y; // 得到键盘弹出后的键盘视图所在y坐标
    
    NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    // 添加移动动画，使视图跟随键盘移动
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:duration.doubleValue animations:^{
            [UIView setAnimationBeginsFromCurrentState:YES];
            [UIView setAnimationCurve:[curve intValue]];
            
            self.center = CGPointMake(self.center.x, keyBoardEndY - self.bounds.size.height/2.0); // keyBoardEndY的坐标包括了状态栏的高度，要减去
        }];
    });

}


#pragma mark - 懒加载
- (UITextView *)personalSignatureTV {
    if (!_personalSignatureTV) {
        _personalSignatureTV = [UITextView new];
        _personalSignatureTV.font = APPFONT(15);
        _personalSignatureTV.textAlignment = NSTextAlignmentLeft;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.personalSignatureTV becomeFirstResponder];
        });
        _personalSignatureTV.autocorrectionType  = UITextAutocorrectionTypeNo; // 自动纠错模式
        _personalSignatureTV.delegate = self;
        _personalSignatureTV.layer.masksToBounds = YES;
        _personalSignatureTV.layer.cornerRadius = 5;
        _personalSignatureTV.layer.borderWidth = 1;
        _personalSignatureTV.layer.borderColor = Color217.CGColor;
        [_personalSignatureTV setValue:@"输入使用积分数" forKeyPath:@"placeholder"];
        [self addSubview:_personalSignatureTV];
        [_personalSignatureTV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(12);
            make.left.equalTo(self).with.offset(18);
            make.right.equalTo(self).with.offset(- 18);
            make.bottom.equalTo(self).with.offset(- 37);
        }];
    }
    return _personalSignatureTV;
}
- (UIButton *)surePersonalSignatureButton {
    if (!_surePersonalSignatureButton) {
        _surePersonalSignatureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _surePersonalSignatureButton.userInteractionEnabled = NO;
        [_surePersonalSignatureButton setTitle:@"确定" forState:UIControlStateNormal];
        [_surePersonalSignatureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _surePersonalSignatureButton.layer.cornerRadius = 3;
        _surePersonalSignatureButton.titleLabel.font = APPFONT(14);
        [_surePersonalSignatureButton addTarget:self action:@selector(progressSurePersonalSignatureButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_surePersonalSignatureButton];
        [_surePersonalSignatureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.personalSignatureTV.mas_bottom).with.offset(5);
            make.right.equalTo(self.personalSignatureTV);
            make.height.mas_equalTo(26);
            make.width.mas_equalTo(53);
        }];
    }
    return _surePersonalSignatureButton;
}

@end
